package com.supermarket.service.impl;

import com.alibaba.excel.EasyExcel;
import com.supermarket.entity.Goods;
import com.supermarket.listener.GoodsListener;
import com.supermarket.mapper.GoodsMapper;
import com.supermarket.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public List<Goods> findGoodsAll() {
        return goodsMapper.findGoodsAll();
    }

    @Override
    public void add(Goods goods) { goodsMapper.add(goods);}

    @Override
    public void deleteById(String id) { goodsMapper.deleteById(id);}

    @Override
    public void updateGoods(Goods goods) { goodsMapper.updateGoods(goods);}

    @Override
    public void batchDel(Integer[] ids) {goodsMapper.batchDel(ids);}
    @Override
    public List<Goods> findByIds(Integer[] ids) {
        return goodsMapper.findByIds(ids);
    }

    @Override
    public void upload(MultipartFile file) {
        InputStream inputStream = null;

        //获取输入流
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(inputStream, Goods.class, new GoodsListener(goodsMapper)).sheet().doRead();


    }

    }



